# Maintainer: Kouhei Sutou <kou@clear-code.com>

_realname=arrow
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0.0
pkgrel=1
pkgdesc="Apache Arrow is a cross-language development platform for in-memory data (mingw-w64)"
arch=("any")
url="https://arrow.apache.org/"
license=("Apache-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-flatbuffers"
         "${MINGW_PACKAGE_PREFIX}-gflags"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
         "${MINGW_PACKAGE_PREFIX}-grpc"
         "${MINGW_PACKAGE_PREFIX}-libutf8proc"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-python3-numpy"
         "${MINGW_PACKAGE_PREFIX}-rapidjson"
         "${MINGW_PACKAGE_PREFIX}-re2"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-thrift"
         "${MINGW_PACKAGE_PREFIX}-uriparser"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-polly")
options=("staticlibs" "strip" "!buildflags")
source=(https://github.com/apache/arrow/archive/apache-arrow-${pkgver}.tar.gz)
sha256sums=('08fbd4c633c08939850d619ca0224c75d7a0526467c721c0838b8aa7efccb270')

cmake_build_type=release
meson_build_type=debugoptimized

source_dir=${_realname}-apache-${_realname}-${pkgver}
cpp_build_dir=build-${CARCH}-cpp
c_glib_build_dir=build-${CARCH}-c-glib

prepare() {
  pushd ${source_dir}
  popd
}

build() {
  [[ -d ${cpp_build_dir} ]] && rm -rf ${cpp_build_dir}
  mkdir -p ${cpp_build_dir}
  pushd ${cpp_build_dir}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      ../${source_dir}/cpp \
      -DARROW_BUILD_UTILITIES=ON \
      -DARROW_FLIGHT=ON \
      -DARROW_GANDIVA=ON \
      -DARROW_PACKAGE_PREFIX=${MINGW_PREFIX} \
      -DARROW_PARQUET=ON \
      -DARROW_PYTHON=ON \
      -DARROW_USE_GLOG=OFF \
      -DARROW_WITH_BROTLI=ON \
      -DARROW_WITH_BZ2=ON \
      -DARROW_WITH_LZ4=ON \
      -DARROW_WITH_SNAPPY=ON \
      -DARROW_WITH_ZLIB=ON \
      -DARROW_WITH_ZSTD=ON \
      -DBOOST_ROOT=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${cmake_build_type} \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DPARQUET_BUILD_EXECUTABLES=ON \
      -G "MSYS Makefiles"
  make
  popd

  [[ -d ${c_glib_build_dir} ]] && rm -rf ${c_glib_build_dir}
  meson \
    ${c_glib_build_dir} \
    ${source_dir}/c_glib \
    --buildtype=${meson_build_type} \
    --prefix=${MINGW_PREFIX} \
    -Darrow_cpp_build_dir=$(pwd)/${cpp_build_dir} \
    -Darrow_cpp_build_type=${cmake_build_type} \
    -Dgtk_doc=true
  sed -i'' -s 's/\r//g' ${c_glib_build_dir}/arrow-glib/version.h
  PATH=$(pwd)/${cpp_build_dir}/${cmake_build_type}:$PATH \
    ninja -C ${c_glib_build_dir}
}

check() {
  # TODO
  # make -C ${cpp_build_dir} test

  # PATH=$(pwd)/${c_glib_build_dir}/arrow-glib:$(pwd)/${cpp_build_dir}/${cmake_build_type}:$PATH \
  #   ninja -C ${c_glib_build_dir} test

  :
}

package() {
  make -C ${cpp_build_dir} DESTDIR="${pkgdir}" install

  PATH=$(pwd)/${c_glib_build_dir}/arrow-glib:$(pwd)/${cpp_build_dir}/${cmake_build_type}:$PATH \
  DESTDIR="${pkgdir}${MINGW_PREFIX}" \
    ninja -C ${c_glib_build_dir} install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  for pc in *.pc; do
    sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i $pc
  done
  popd
}
